package com.ruoyi.mateproduction.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateproduction.domain.MateDaily;
import com.ruoyi.mateproduction.service.MateDailyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */
@RestController
@RequestMapping("/mateDaily")
public class MateDailyController  extends BaseController {

    @Autowired
    MateDailyService mateDailyService;
    /**
     * 保存生产日报表
     */
    @Log(title = "生产日报表", businessType = BusinessType.INSERT)
    @PostMapping("/addMateDaily")
    public AjaxResult addMateDaily(@RequestBody MateDaily mateDaily) {
        mateDaily.setDeptId(SecurityUtils.getLoginUser().getCompanyId());
        int num = mateDailyService.addMateDaily(mateDaily);
        if (num > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败");
        }
    }

    /*
  查询所有生产日报表
   */
    @GetMapping("/dailyList")
    @ResponseBody
    public TableDataInfo dailyList(@RequestParam(value = "beginTime", required = false) String beginTime,
                                        @RequestParam(value = "endTime", required = false) String endTime) {
        startPage();
        MateDaily mateDaily = new MateDaily();
        mateDaily.setBeginTime(beginTime);
        mateDaily.setEndTime(endTime);
        mateDaily.setDeptId(SecurityUtils.getDeptId());
        List<MateDaily> list = mateDailyService.dailyList(mateDaily);
        return getDataTable(list);
    }

    @GetMapping("/selectEdit")
    @ResponseBody
    public Map<String, Object> selectEdit(@RequestParam(value = "dailyId", required = false) Long dailyId) {
        Map<String, Object> resMap = new HashMap<>();
        MateDaily mateDaily = mateDailyService.selectMateDaily(dailyId);
        resMap.put("result", mateDaily);
        return resMap;
    }

    /**
     * 删除生产日报表
     */
    @Log(title = "删除生产日报表", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{dailyId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("dailyId") Long dailyId) {
        mateDailyService.removeDaily(dailyId);
        return AjaxResult.success("删除成功");
    }


    /*
  编辑批生产记录
     */
    @PostMapping("/editDaily")
    @ResponseBody
    public AjaxResult editDaily(@RequestBody MateDaily mateDaily) {
        int num = mateDailyService.editDaily(mateDaily);
        if (num > 0) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error("修改失败");
        }
    }


}
